package com.example.service.impl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.dao.BusinessDataSynchronizationMapper;
import com.example.dao.ProjectInfoMapper;
import com.example.service.BusinessDataSynchronizationService;

@Service
public class BusinessDataSynchronizationServiceImpl implements BusinessDataSynchronizationService {

	@Autowired
	private BusinessDataSynchronizationMapper mapperBean;

	@Autowired
	private ProjectInfoMapper projectInfoMapper;

	@Override
	public void jobcard_cut_QCDatasynchronization() {

		List<Map<String, String>> projectInfoMaps = projectInfoMapper.select_ProjectID_Number(); // 查找出所有项目

		for (Map<String, String> projectInfoMap : projectInfoMaps) { // 遍历项目

			Object object = projectInfoMap.get("PID");
			int projectId = Integer.parseInt(String.valueOf(object));

			List<Map<String, String>> datas = mapperBean.select_jobcard_cut_result(projectId); // 查询所有未同步过结构的零件报告
			if (!datas.isEmpty()) {
				for (Map<String, String> data : datas) {
					mapperBean.update_jobcard_cut_result(data, projectId);// 同步报检结构
					mapperBean.update_jobcard_cut_result_status(data, "yes"); // 改为已同步
				}
			}
		}
	}

	@Override
	public void jobcard_fitup_QCDatasynchronization() {
		List<Map<String, String>> projectInfoMaps = projectInfoMapper.select_ProjectID_Number(); // 查找出所有项目

		for (Map<String, String> projectInfoMap : projectInfoMaps) { // 遍历项目

			Object object = projectInfoMap.get("PID");
			int projectId = Integer.parseInt(String.valueOf(object));

			List<Map<String, String>> datas = mapperBean.select_jobcard_fitup_result(projectId); // 查询所有未同步过结构的零件报告
			if (!datas.isEmpty()) {
				for (Map<String, String> data : datas) {
					mapperBean.update_jobcard_fitup_result(data, projectId);// 同步报检结构
					mapperBean.update_jobcard_fitup_result_status(data, "yes"); // 改为已同步
				}
			}
		}

	}

	@Override
	public void jobcard_weld_QCDatasynchronization() {
		List<Map<String, String>> projectInfoMaps = projectInfoMapper.select_ProjectID_Number(); // 查找出所有项目

		for (Map<String, String> projectInfoMap : projectInfoMaps) { // 遍历项目
			Object object = projectInfoMap.get("PID");
			int projectId = Integer.parseInt(String.valueOf(object));
			List<Map<String, String>> datas = mapperBean.select_jobcard_weld_result(projectId); // 查询所有未同步过结构的零件报告
			if (!datas.isEmpty()) {
				for (Map<String, String> data : datas) {
					mapperBean.update_jobcard_weld_result(data, projectId);// 同步报检结构
					mapperBean.update_jobcard_weld_result_status(data, "yes"); // 改为已同步
				}
			}
		}
	}

	@Override
	public void delete_jobcard_QCDatasynchronization() {
		mapperBean.delete_jobcard_cut_result();
		mapperBean.delete_jobcard_weld_result();
		mapperBean.delete_jobcard_fitup_result();
	}

	@Override
	public void update_RealTimeInventory() {
		List<Map<String, String>> projectInfoMaps = projectInfoMapper.select_ProjectID_Number(); // 查找出所有项目

		for (Map<String, String> projectInfoMap : projectInfoMaps) { // 遍历项目
			Object object = projectInfoMap.get("PID");
			int projectId = Integer.parseInt(String.valueOf(object));
			//先同步领料单中的数据
			List<Map<String, String>> materials =	mapperBean.select_MaterialInventoryPickingList(projectId);//实仓领料单
			List<Map<String, String>> materials2 =	mapperBean.select_ImportAndExportMaterialInventoryPickingList(projectId);//虚仓领料单
			if(materials.size()>0) {
			mapperBean.update_ReleaseLockRealTimeInventory(projectId, materials);//将相应的领料单数据对及时库存进行同步
			projectInfoMapper.update_data_status("UNIFIER_UXPAD_LINEITEM","yes", materials); //将数据状态改为已同步
			}
			if(materials2.size()>0) {
			mapperBean.update_ReleaseLockRealTimeInventory(projectId, materials2);//将相应的领料单数据对及时库存进行同步
			projectInfoMapper.update_data_status("UNIFIER_UXIAEWO_LINEITEM","yes",materials2);//将数据状态改为已同步
			}
		 
		 
			
			List<Map<String, String>> ledgerMaterials = mapperBean.select_MaterialInventoryLedger(projectId);// 物料管理台账
			ledgerMaterials.addAll( mapperBean.select_ImportAndExportMaterialInventoryLedger(projectId));// 进出口台账信息
			if(ledgerMaterials.size()>0) {
			mapperBean.update_RealTimeInventory(projectId,ledgerMaterials);//将台账信息同步进即时库存 因台账信息与库存是实时需同步则无需将状态改为已同步
			}
			
		}

	}

	@Override
	public void update_RealTimeInventoryJobcard() {
		List<Map<String, String>> projectInfoMaps = projectInfoMapper.select_ProjectID_Number(); // 查找出所有项目
		for (Map<String, String> projectInfoMap : projectInfoMaps) { // 遍历项目
			Object object = projectInfoMap.get("PID");
			int projectId = Integer.parseInt(String.valueOf(object));
			
		   List<Map<String, String>>  cutDatas= mapperBean.select_jobcard_cut_MaterialProduced(projectId);
		   if(cutDatas.size()>0) {
		   mapperBean.update_RealTimeInventory_MaterialSort(projectId, "part", cutDatas); // fabrication  bulk part
		   cutDatas =  cutDatas.stream().filter(Util.distinctByKey(deviceRecord -> deviceRecord.get("ID"))).collect(Collectors.toList());//将去重
		   projectInfoMapper.update_data_status("UNIFIER_UXJBMAC","yes",cutDatas);//将数据状态改为已同步
		   }
		   List<Map<String, String>>  weldDatas= mapperBean.select_jobcard_Weld_MaterialProduced(projectId);
		   if(weldDatas.size()>0) {
		   mapperBean.update_RealTimeInventory_MaterialSort(projectId, "fabrication", weldDatas); // fabrication  bulk part
		   projectInfoMapper.update_data_status("UNIFIER_UXJBWED","yes",weldDatas);//将数据状态改为已同步
		   }
		 
		}
	}

 
 
	
	
}
